<template>
  <view class="membership-card" :class="[membershipClass]">
    <!-- 背景 -->
    <view class="card-bg">
      <view class="card-pattern"></view>
      <view class="card-glow"></view>
    </view>
    
    <!-- 内容 -->
    <view class="card-content">
      <!-- 头部信息 -->
      <view class="card-header">
        <view class="user-info">
          <view class="level-name">{{ levelName }}</view>
          <view class="level-tag" :class="{ 'vip-tag': isVip }">{{ isVip ? 'VIP会员' : '免费用户' }}</view>
        </view>
        <text class="expire-time" v-if="isVip">{{ formatExpireTime }}</text>
      </view>

      <!-- 权益信息 -->
      <view class="card-body">
        <view class="quota-item">
          <text class="quota-value">{{ formatNumber(remainingTokens) }}</text>
          <text class="quota-label">剩余Token</text>
        </view>
        <view class="quota-item">
          <text class="quota-value">{{ remainingChatCount }}</text>
          <text class="quota-label">剩余对话</text>
        </view>
        <view class="quota-item">
          <text class="quota-value">{{ remainingSwitchCount }}</text>
          <text class="quota-label">剩余切换</text>
        </view>
      </view>

      <!-- 底部操作 -->
      <view class="card-footer">
        <button class="action-btn" @tap="handleAction">
          {{ isVip ? '续费会员' : '开通会员' }}
        </button>
      </view>
    </view>
  </view>
</template>

<script setup>
import { computed } from 'vue'

const props = defineProps({
  membershipInfo: {
    type: Object,
    default: () => ({})  
  }
})

const emit = defineEmits(['action'])

const isVip = computed(() => Boolean(props.membershipInfo?.isVip))
const levelName = computed(() => props.membershipInfo?.levelName || '免费用户')
const remainingTokens = computed(() => props.membershipInfo?.dailyTokenLimit || 0)
const remainingChatCount = computed(() => props.membershipInfo?.remainingChatCount || 0)
const remainingSwitchCount = computed(() => props.membershipInfo?.remainingSwitchCount || 0)

// 卡片样式
const membershipClass = computed(() => isVip.value ? 'vip-card' : 'free-card')

// 格式化到期时间
const formatExpireTime = computed(() => {
  if (!props.membershipInfo?.expireTime) return ''
  const date = new Date(props.membershipInfo.expireTime)
  return `有效期至 ${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`
})

// 格式化数字(添加千分位)
const formatNumber = (num) => {
  return String(num).replace(/\B(?=(\d{3})+(?!\d))/g, ',')
}

// 处理操作按钮点击
const handleAction = () => {
  emit('action', isVip.value ? 'renew' : 'subscribe')
}
</script>

<style lang="scss">
.membership-card {
  width: 100%;
  height: 380rpx;
  border-radius: 24rpx;
  position: relative;
  overflow: hidden;
  margin-bottom: 30rpx;
  box-shadow: 0 15rpx 30rpx rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  transform: perspective(800px) rotateX(0deg);
  
  &:active {
    transform: perspective(800px) rotateX(3deg) translateY(5rpx);
    box-shadow: 0 10rpx 20rpx rgba(0, 0, 0, 0.08);
  }

  // 背景
  .card-bg {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 1;
    overflow: hidden;
    
    .card-pattern {
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      opacity: 0.1;
      background-size: cover;
      background-position: center;
    }
    
    .card-glow {
      position: absolute;
      width: 200%;
      height: 200%;
      top: -50%;
      left: -50%;
      background: radial-gradient(circle at center, rgba(255,255,255,0.2) 0%, rgba(255,255,255,0) 60%);
      transform: rotate(15deg);
      animation: glow 8s linear infinite;
    }
  }

  // VIP会员卡样式
  &.vip-card {
    .card-bg {
      background: linear-gradient(135deg, #5677fc, #38a0ff);
      
      .card-pattern {
        background-image: url('/static/images/vip-pattern.png');
      }
    }
  }

  // 免费用户卡样式
  &.free-card {
    .card-bg {
      background: linear-gradient(135deg, #999, #666);
      
      .card-pattern {
        background-image: url('/static/images/vip-pattern.png');
        opacity: 0.05;
      }
    }
  }

  // 内容区域
  .card-content {
    position: relative;
    z-index: 2;
    padding: 40rpx;
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
  }

  // 头部信息
  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;

    .user-info {
      .level-name {
        font-size: 42rpx;
        color: #fff;
        font-weight: 600;
        margin-bottom: 16rpx;
        text-shadow: 0 2rpx 4rpx rgba(0, 0, 0, 0.1);
      }

      .level-tag {
        display: inline-block;
        font-size: 24rpx;
        color: rgba(255, 255, 255, 0.9);
        background: rgba(255, 255, 255, 0.2);
        padding: 4rpx 16rpx;
        border-radius: 20rpx;
        backdrop-filter: blur(5px);
        
        &.vip-tag {
          background: rgba(255, 215, 0, 0.3);
          border: 1px solid rgba(255, 215, 0, 0.5);
        }
      }
    }

    .expire-time {
      font-size: 24rpx;
      color: rgba(255, 255, 255, 0.9);
      padding: 8rpx 16rpx;
      background: rgba(0, 0, 0, 0.1);
      border-radius: 30rpx;
    }
  }

  // 权益信息
  .card-body {
    display: flex;
    justify-content: space-between;
    margin: 30rpx 0;

    .quota-item {
      display: flex;
      flex-direction: column;
      align-items: center;
      padding: 20rpx 0;
      position: relative;
      
      &::after {
        content: '';
        position: absolute;
        top: 10%;
        right: -30rpx;
        width: 1px;
        height: 80%;
        background: rgba(255, 255, 255, 0.2);
      }
      
      &:last-child::after {
        display: none;
      }

      .quota-label {
        font-size: 24rpx;
        color: rgba(255, 255, 255, 0.9);
        margin-top: 10rpx;
      }

      .quota-value {
        font-size: 42rpx;
        color: #fff;
        font-weight: 600;
        text-shadow: 0 2rpx 4rpx rgba(0, 0, 0, 0.1);
      }
    }
  }

  // 底部操作
  .card-footer {
    display: flex;
    justify-content: center;
    
    .action-btn {
      width: 240rpx;
      height: 80rpx;
      line-height: 80rpx;
      background: rgba(255, 255, 255, 0.15);
      color: #fff;
      font-size: 28rpx;
      border-radius: 40rpx;
      text-align: center;
      backdrop-filter: blur(5px);
      border: 1px solid rgba(255, 255, 255, 0.3);
      box-shadow: 0 5rpx 15rpx rgba(0, 0, 0, 0.1);
      transition: all 0.3s ease;

      &:active {
        transform: translateY(2rpx);
        box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
        background: rgba(255, 255, 255, 0.25);
      }
    }
  }
}

@keyframes glow {
  0% {
    transform: rotate(15deg) translateX(-30%) translateY(10%);
  }
  50% {
    transform: rotate(15deg) translateX(10%) translateY(-5%);
  }
  100% {
    transform: rotate(15deg) translateX(-30%) translateY(10%);
  }
}
</style>